package com.star.controller.user;

import com.star.dto.OrdersHistoryQueryDTO;
import com.star.dto.OrdersPaymentDTO;
import com.star.dto.OrdersSubmitDTO;
import com.star.entity.Orders;
import com.star.result.PageResult;
import com.star.result.Result;
import com.star.service.OrderService;
import com.star.vo.OrderPaymentVO;
import com.star.vo.OrderSubmitVO;
import com.star.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("userOrderController")
@RequestMapping("/user/order")
@Slf4j
@Api(tags = "C端订单管理相关接口")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 用户下单
     * @param ordersSubmitDTO
     * @return
     */
    @PostMapping("/submit")
    @ApiOperation("用户下单")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        OrderSubmitVO orderSubmitVO = orderService.submit(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }

    /**
     * 历史订单查询
     * @param ordersHistoryQueryDTO   订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消
     * @return
     */
    @GetMapping("/historyOrders")
    @ApiOperation("历史订单查询")
    public Result<PageResult> page(OrdersHistoryQueryDTO ordersHistoryQueryDTO) {
        log.info("历史订单查询{}", ordersHistoryQueryDTO);
        PageResult pageResult = orderService.userOrderHistory(ordersHistoryQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 查询订单详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("查询订单详情")
    public Result<OrderVO> detail(@PathVariable Long id){
        log.info("查询订单详情{}", id);
        OrderVO orderVO = orderService.userOrderDetail(id);
        return Result.success(orderVO);
    }

    /**
     * 删除订单
     * @return
     */
    @DeleteMapping
    public Result<String> delete(@RequestParam Long id){
        log.info("删除订单{}",id);
        orderService.delete(id);
        return Result.success();
    }
    /**
     * 取消订单
     * @param id
     * @return
     */
    @PutMapping("/cancel/{id}")
    @ApiOperation("取消订单")
    public Result<String> cancel(@PathVariable Long id) throws Exception {
        log.info("取消订单{}", id);
        orderService.userCancel(id);
        return Result.success();
    }

    /**
     * 订单支付(需要微信小程序商务号)
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    /**
     * 模拟支付
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/myPayment")
    @ApiOperation("订单支付")
    public Result<String> myPayment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) {
        log.info("订单支付{}", ordersPaymentDTO);
        orderService.myPayment(ordersPaymentDTO);
        return Result.success();
    }

    /**
     * 客户催单
     * @param id
     * @return
     */
    @GetMapping("/reminder/{id}")
    @ApiOperation("客户催单")
    public Result<String> reminder(@PathVariable Long id){
        log.info("客户催单{}", id);
        orderService.reminder(id);
        return Result.success();
    }

    /**
     * 再来一单
     * @param id
     * @return
     */
    @PostMapping("/repetition/{id}")
    @ApiOperation("再来一单")
    public Result<String> repetition(@PathVariable Long id){
        log.info("再来一单{}", id);
        orderService.repetiton(id);
        return Result.success();
    }


}
